<?php
/**
 * 
 * 
 * @version $Id: Sina.php 9 2012-08-31 10:06:37Z linsir123 $
 */
class weiboSina implements WeiboBase
{
	/**
	 * @var 配置
	 */
	public $conf;
	
	/**
	 * @var 回调
	 */
	public $callback;
	
	/**
	 * @var Key
	 */
	public $sessionKey = 'sina_access_token';
	
	/**
	 * @var Token
	 */
	public $accessToken;
	
	/**
	 * @var 授权时间
	 */
	public $oauthTime;
	
	/**
	 * @var 
	 */
	public $uId;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		session_start();
		
		///
		$this->conf = App::O('app.sina');
		$this->callback = lpUrl::__($this->conf['callback'], null, true);
		$this->_init();
	}
	
	/**
	 * 取得授权地址
	 */
	public function getOauthUrl()
	{
		$url = "https://api.weibo.com/oauth2/authorize";
		$data = array(
			'client_id' 	=> $this->conf['key'][0],
			'response_type'	=> 'code',
			'redirect_uri'	=> $this->callback,
		);
		$url .= "?". http_build_query($data);
		
		///
		return $url;
	}
	
	/**
	 * 回调操作
	 */
	public function doCallback()
	{
		if (isset($_GET['code'])) {
			$url = "https://api.weibo.com/oauth2/access_token";
			$data = array (
				'client_id'		=> $this->conf['key'][0],
				'client_secret'	=> $this->conf['key'][1],
				'grant_type' 	=> 'authorization_code',
				'redirect_uri'	=> $this->callback,
				'code' 			=> $_GET['code'],
			);
			
			///
			$tmp = $this->_curl($url, $data, false);
			if (isset($tmp['access_token'])) {
				$this->_init($tmp['access_token']);
				return true;
			}
		}
		
		///
		return false;
	}
	
	/**
	 * 验证是否登录
	 */
	public function isLogin()
	{
		if ( ! empty($this->accessToken) 
			&& $this->oauthTime > 0 
			&& (time() - $this->oauthTime < 24*3600)) 
			return true;
			
		///
		return false;
	}
	
	/**
	 * 退出
	 */
	public function logout()
	{
		@session_destroy();
		///
		/*$this->_curl("https://api.weibo.com/2/account/end_session.json", array(
			'access_token' => $this->accessToken
		));*/
	}
	
	/**
	 * 取得当前登录的用户
	 */
	public function getUser($uId = 0)
	{
		if (empty($uId)) $uId = $this->uId;
		///
		return $this->_curl("https://api.weibo.com/2/users/show.json", array(
			'access_token' => $this->accessToken,
			'uid' => $uId,
		));
	}
	
	/**
	 * 取得关注列表
	 */
	public function getFriends($uId = 0, $count = 10, $cursor = 0)
	{
		if (empty($uId)) $uId = $this->uId;
		///
		return $this->_curl("https://api.weibo.com/2/friendships/friends.json", array(
			'access_token' => $this->accessToken,
			'uid' => $uId,
			'count' => $count,
			'cursor' => $cursor,
			'trim_status' => 0
		));
	}
	
	/**
	 * 取得关注列表
	 */
	public function getFriendsIds($uId = 0, $count = 10, $cursor = 0)
	{
		if (empty($uId)) $uId = $this->uId;
		///
		return $this->_curl("https://api.weibo.com/2/friendships/friends/ids.json", array(
			'access_token' => $this->accessToken,
			'uid' => $uId,
			'count' => $count,
			'cursor' => $cursor
		));
	}
	
	/**
	 * 取消关注一个用户
	 */
	public function destroy($uId)
	{
		return $this->_curl("https://api.weibo.com/2/friendships/destroy.json", array(
			'access_token' => $this->accessToken,
			'uid' => $uId
		), false);
	}
	
	
	///
	
	
	/**
	 * 
	 */
	private function _curl($url, $data = null, $methodGet = true)
	{
		$url = trim($url);
		if ($methodGet && ! empty($data)) {
			$tmp = array();
			foreach ($data as $k => $v) 
				$tmp[] = "{$k}={$v}";
				
			///
			$data = null;
			$url .= "?". implode("&", $tmp);
		}
		
		///
		$tmp = lpApi::curl($url, $data, true);
		if ( ! empty($tmp)) 
			$tmp = json_decode($tmp, true);
			
		///
		return $tmp;
	}
	
	/**
	 * 
	 */
	private function _init($token = null)
	{
		if ( ! empty($token)) {
			$url = "https://api.weibo.com/2/account/get_uid.json?access_token=". $token;
			$tmp = $this->_curl($url);
			if (isset($tmp['uid'])) {
				$_SESSION[$this->sessionKey] = array(
					'accessToken' => $token,
					'oauthTime' => time(),
					'uId' => $tmp['uid']
				);
			}
		}
		
		///
		if (isset($_SESSION[$this->sessionKey])) {
			$data = $_SESSION[$this->sessionKey];
			$this->accessToken = $data['accessToken'];
			$this->oauthTime = $data['oauthTime'];
			$this->uId = $data['uId'];
		}
	}
}
